package com.readen.leetcode;

/**
 * 求一个数的开方
 * @author Administrator
 *
 */
public class Sqirter {

	public static double sqrt(double x){
		double ret=1;
		int count=0;
		while(Math.abs(x-ret*ret)>0.0000000001){
			ret*=1+0.5*(x/(ret*ret)-1);
			count++;
		}
		System.out.println(count);
		return ret;
	}
	
	public static double sqrt2(double x){
		double ret=1;
		double ratio=1;
		double count=0;
		
		while(Math.abs(x-ret*ret)>0.0000000001){
			while(ret*ret-x<0){
				ret+=ratio*ret;
				count++;
			}
			ratio=ratio*1/2;
			while(ret*ret-x>0){
				ret-=ratio*ret;
				count++;
			}
			ratio=ratio*1/2;
		}
		System.out.println(count);
		return ret;
	}
	
	
}
